package com.yjt.fresh.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyStores;
import com.yjt.fresh.api.ApiGetStores;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanProduct;
import com.yjt.fresh.bean.BeanStore;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 店铺列表
 * Created by mcc on 2018/6/16.
 */

public class AtyStores extends BaseSimpleActivity {
    private static final int PAGE_SIZE = 10;
    private static final String GENERAL = "0";
    private static final String SALE_QUANTITY = "1";
    @BindView(R.id.rl_app_aty_store_back)
    RelativeLayout mRlBack;
    @BindView(R.id.tv_app_aty_stores_search)
    TextView mTvSearch;
    @BindView(R.id.btn_app_aty_store_search)
    Button mBtnSearch;
    @BindView(R.id.tv_app_aty_products_general)
    TextView mTvGeneral;
    @BindView(R.id.tv_app_aty_products_salesQuantity)
    TextView mTvQuantity;
    @BindView(R.id.tv_app_aty_products_distance)
    TextView mTvDistance;
    @BindView(R.id.rv_app_aty_stores)
    RecyclerView mRvStores;
    @BindView(R.id.vAppAtyStoresGeneral)
    View mVGeneral;
    @BindView(R.id.rlAppAtyStoresGeneral)
    RelativeLayout mRlGeneral;
    @BindView(R.id.vAppAtyStoresSaleQuantity)
    View mVSaleQuantity;
    @BindView(R.id.rlAppAtyStoresSaleQuantity)
    RelativeLayout mRlSaleQuantity;
    @BindView(R.id.vAppAtyStoresDistance)
    View mVDistance;
    @BindView(R.id.rlAppAtyStoresDistance)
    RelativeLayout mRlDistance;
    @BindView(R.id.refreshLayoutAppAtyStores)
    SwipeRefreshLayout mRefreshLayout;
    private AdpRvAtyStores mAdpStores;
    private Map<String, Object> mParamMap;
    private int mNextRequestPage =1;
    private boolean isSortByDistance;
    private boolean isShowProgress;
    private boolean isLoadMore;
    private String mOrderType = "0";

    @Override
    protected int getContentView() {
        return R.layout.app_aty_stores;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        String keyWord = getIntent().getStringExtra(MyConstants.STORE_KEY_WORD);
        mTvSearch.setText(keyWord);
    }

    @Override
    protected void initEvent() {
        initRv();
        initClick();
        initParam();
        getData();
        initRefresh();
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRvStores.setLayoutManager(manager);
        List<BeanStore> list = new ArrayList<>();
        mAdpStores = new AdpRvAtyStores(R.layout.app_recycle_item_stores, list);
        mRvStores.setAdapter(mAdpStores);
    }

    private void initClick() {

        mTvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtySearch.class);
                startActivity(intent);
            }
        });
        mAdpStores.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext,AtyStoreDetail.class);
                intent.putExtra(MyConstants.STORE_ID,mAdpStores.getItem(position).getSTORE_ID());
                intent.putExtra(MyConstants.STORE_NAME,mAdpStores.getItem(position).getSTORE_NAME());
                intent.putExtra(MyConstants.STORE_LOGO,mAdpStores.getItem(position).getLOGO_IMG());
                intent.putExtra(MyConstants.STORE_LOCATION,mAdpStores.getItem(position).getLATITUDE_LONGITUDE());
                startActivity(intent);
            }
        });
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRlGeneral.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvGeneral.setTextColor(getMyColor(R.color.color_43b624));
                mTvDistance.setTextColor(getMyColor(R.color.color_333333));
                mTvQuantity.setTextColor(getMyColor(R.color.color_333333));

                mVGeneral.setVisibility(View.VISIBLE);
                mVSaleQuantity.setVisibility(View.GONE);
                mVDistance.setVisibility(View.GONE);
                isLoadMore = false;
                isSortByDistance = false;
                mNextRequestPage = 1;
                mOrderType = GENERAL;
                getData();
            }
        });

        mRlSaleQuantity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvQuantity.setTextColor(getMyColor(R.color.color_43b624));
                mTvDistance.setTextColor(getMyColor(R.color.color_333333));
                mTvGeneral.setTextColor(getMyColor(R.color.color_333333));

                mVSaleQuantity.setVisibility(View.VISIBLE);
                mVGeneral.setVisibility(View.GONE);
                mVDistance.setVisibility(View.GONE);
                isLoadMore = false;
                isSortByDistance = false;
                mNextRequestPage = 1;
                mOrderType = SALE_QUANTITY;
                getData();
            }
        });

        mRlDistance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDistance.setTextColor(getMyColor(R.color.color_43b624));
                mTvQuantity.setTextColor(getMyColor(R.color.color_333333));
                mTvGeneral.setTextColor(getMyColor(R.color.color_333333));

                mVDistance.setVisibility(View.VISIBLE);
                mVGeneral.setVisibility(View.GONE);
                mVSaleQuantity.setVisibility(View.GONE);
                sortByDistance();
            }
        });
    }

    private void sortByDistance() {
        for (int i = 0; i < mAdpStores.getData().size(); i++) {
            PreferenceManager manager = PreferenceManager.getInstance();
            BeanStore beanProduct = mAdpStores.getData().get(i);
            beanProduct.setLocalLatitude(Double.valueOf(manager.getLatitude()));
            beanProduct.setLocalLongitude(Double.valueOf(manager.getLongitude()));

        }
        Collections.sort(mAdpStores.getData());
        mAdpStores.notifyDataSetChanged();
        mRvStores.scrollToPosition(0);
    }
    private int getMyColor(int colorResID) {
        return getResources().getColor(colorResID);
    }

    private void initParam(){
        String keyWord = getIntent().getStringExtra(MyConstants.STORE_KEY_WORD);
        mParamMap = new HashMap<>();
        mParamMap.put("currentPage", mNextRequestPage);
        mParamMap.put("keywords", keyWord);
        mParamMap.put("ORDERTYPE",mOrderType);
    }


    HttpOnNextListener<List<BeanStore>> mStoresListener = new HttpOnNextListener<List<BeanStore>>() {
        @Override
        public void onNext(List<BeanStore> beanStores) {
            mAdpStores.setEmptyView(R.layout.view_empty);
            isShowProgress = true;
            if (isLoadMore) {
                setData(false, beanStores);
            } else {
                setData(true, beanStores);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void setData(boolean isRefresh, List<BeanStore> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpStores.setNewData(data);
        } else {
            if (size > 0) {
                mAdpStores.addData(data);
                mAdpStores.loadMoreComplete();
            }else {
                mAdpStores.loadMoreEnd(true);
            }
        }
    }
    private void getData() {
        initParam();
        ApiGetStores apiGetStores = new ApiGetStores(mStoresListener,mContext,true,false,true);
        apiGetStores.setMap(mParamMap);
        HttpManager.getInstance().doHttpDeal(apiGetStores);
    }

    private void initRefresh() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                isShowProgress = false;
                getData();
            }
        });

        mAdpStores.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        },mRvStores);
    }
}
